<template>
  <div class="login-container">
    <el-row :gutter="20">
      <el-col :span="12"
        ><div class="left">
          <img
            src="../../static/logod.png"
            alt=""
            style="
              width: 253px;
              height: 56px;
              margin-right: 5px;
              margin-top: -250px;
            "
          />
          <div class="flex">
            <span> Hi,你好 </span>
            <span> 欢迎进入物流系统 </span>
            <span style="color: #ffffff; font-size: 36px">
              Welcome to the Logistics System</span
            >
          </div>
        </div></el-col
      >
      <el-col :span="12"
        ><div class="right">
          <span
            style="
              color: #333333;
              font-size: 24px;
              padding-bottom: 30px;
              font-weight: bold;
            "
            >重置密码</span
          >
          <div class="dis">
            <el-input placeholder="请输入手机号" v-model="loginForm.username">
              <i slot="prefix" class="el-input__icon el-icon-mobile-phone"></i>
            </el-input>
            <el-input placeholder="请输入新密码" v-model="loginForm.password">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
            <el-input v-model="loginForm.verificationCode" style="width: 303px">
              <template slot="append">
                <el-button
                  class="code"
                  v-if="loginForm.showloginCode"
                  type="primary"
                  @click="getloginPhoneCode"
                  >获取验证码</el-button
                >
                <div v-else>{{ loginForm.count }}</div>
              </template>
              <i slot="prefix" class="el-input__icon el-icon-key"></i>
            </el-input>
          </div>

          <el-button
            type="primary"
            class="btn"
            :loading="loading"
            @click.native.prevent="handleLogin"
            >确认重置</el-button
          >
        </div></el-col
      >
    </el-row>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("用户名的长度不能小于5"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码的长度不能小于6"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "admin123",
        checked: true,
        verificationCode: "", //表单中展示的验证码
        showloginCode: true, //判断展示‘获取验证码’或‘倒计时’
        count: "", //倒计时时间
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 获取手机验证码
    getloginPhoneCode() {
      // 如果未输入手机号，结束执行
      if (this.loginForm.phone == "") {
        return;
      }
      // 获取随机数（4位数字）
      var numCode = "";
      for (var i = 0; i < 4; i++) {
        numCode += Math.floor(Math.random() * 10);
      }
      // 存储发送的验证码,用于验证输入的手机验证码是否和本地存储的相同
      this.loginForm.contenttext = numCode;
      // 向手机号发送验证码传入的参数
      let phoneCode = {
        phonenum: this.loginForm.phone,
        contenttext:
          "您正在修改密码，验证码为:" + numCode + "，切勿将验证码泄露给他人。",
      };
      // 调用接口，向手机号发送验证码
      this.$axios.post("接口地址", phoneCode).then((res) => {
        if (res.status != 200) {
          return this.$message.error("验证码发送失败！");
        } else {
          // 当验证码发送成功，开始60秒倒计时
          const TIME_COUNT = 60;
          if (!this.loginForm.timer) {
            this.loginForm.showloginCode = false;
            this.loginForm.count = TIME_COUNT;
            this.loginForm.timer = setInterval(() => {
              if (
                this.loginForm.count > 0 &&
                this.loginForm.count <= TIME_COUNT
              ) {
                this.loginForm.count -= 1;
              } else {
                this.loginForm.showloginCode = true;
                clearInterval(this.loginForm.timer);
                this.loginForm.timer = null;
              }
            }, 1000);
          }
        }
      });
    },
    handleLogin() {
      // this.$refs.loginForm.validate((valid) => {
      //   if (valid) {
      //     this.loading = true;
      //     this.$store
      //       .dispatch("user/login", this.loginForm)
      //       .then(() => {
      //         this.$router.push({ path: this.redirect || "/" });
      //         this.loading = false;
      //       })
      //       .catch(() => {
      //         this.loading = false;
      //       });
      //   } else {
      //     console.log("error submit!!");
      //     return false;
      //   }
      // });
      this.loading = true;
      this.$store
        .dispatch("user/login", this.loginForm)
        .then(() => {
          this.$router.push({ path: this.redirect || "/" });
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: black;
$cursor: #fff;

.el-input-group__append,
.el-input-group__prepend {
  border: none;
  background: none;
}

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container {
  height: 100%;
  background-image: url("../../static/beijin.png");
  background-size: 100% 100%;
}
.el-row {
  margin-bottom: 20px;
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: flex;
    height: 56px;
    width: 422px;
    align-items: center;

    input {
      background: transparent;
      border: 1px solid #f0f1f5;
      -webkit-appearance: none;
      border-radius: 0px;
      color: $light_gray;
      height: 47px;
      caret-color: black;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .left {
    display: flex;
    flex-flow: column;
    height: 90vh;
    justify-content: space-around;
    margin-left: 100px;
  }

  .flex {
    display: flex;
    flex-flow: column;
    justify-content: space-between;
    margin-top: -400px;
    height: 300px;
    color: #ffffff;
    font-size: 84px;
  }
  @media screen and (max-width: 600px) {
    .flex {
      font-size: 60px;
    }
  }
  .dis {
    display: flex;
    flex-flow: column;
    height: 220px;
    justify-content: space-around;
  }
  .f-flex {
    display: flex;
    width: 422px;
    justify-content: space-between;
    margin-top: 10px;
  }
  .btn {
    width: 422px;
    height: 56px;
    margin-top: 50px;
  }
  .right {
    width: 550px;
    height: 542px;
    background: white;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-flow: column;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .code {
    background: #2087d0;
    color: white;
    border-radius: 20px;
    margin-left: 25px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
